package demo.service.Impl;

import demo.entity.Student;
import demo.service.StudentService;

import java.io.*;
import java.util.ArrayList;
import java.util.List;


public class StudentServiceImpl implements StudentService {

    //序列化(存)
    public void save(List<Student> students){
        try ( ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("demo18\\src\\student.txt"));){
           oos.writeObject(students);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //反序列化(取)
    public List<Student> get(){

        if(!new File("demo18\\src\\student.txt").exists()){
            return new ArrayList<>();
        }
        //1、读取文件中的数据
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("demo18\\src\\student.txt"));){
            List<Student>  students = (List<Student>) ois.readObject();
            return students;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean add(Student student) {
        //1、文件中读取集合数据
        List<Student> studentList = get();
        //2、将新对象添加到集合中
        studentList.add(student);
        //3、覆盖之前的list集合
        save(studentList);
        return true;
    }

    @Override
    public boolean update(Student student) {
        int index = exists(student.getId());
        if(index != -1){
            //1、文件中读取集合数据
            List<Student> studentList = get();
            //2、将新对象添加到集合中
            studentList.set(index,student);
            //3、覆盖之前的list集合
            save(studentList);
            return true;
        }
        return false;
    }

    @Override
    public boolean delete(String id) {
        int index = exists(id);
        if(index != -1){
            //1、文件中读取集合数据
            List<Student> studentList = get();
            //2、将新对象添加到集合中
            studentList.remove(index);
            //3、覆盖之前的list集合
            save(studentList);
            return true;
        }
        return false;
    }

    @Override
    public Student selectOne(String id) {
        int index = exists(id);
        if(index != -1){
            //1、文件中读取集合数据
            List<Student> studentList = get();
            return studentList.get(index);
        }
        return null;
    }

    @Override
    public List<Student> selectAll() {
        return get();
    }

    @Override
    public List<Student> selectByName(String searchWord) {
        //1、文件中读取集合数据
        List<Student> studentList = get();
        List<Student> newStudentList = new ArrayList<>();
        for (int i = 0; i < studentList.size(); i++) {
            if(studentList.get(i).getName().contains(searchWord)){
                newStudentList.add(studentList.get(i));
            }
        }
        return newStudentList;
    }

    @Override
    public int exists(String id) {
        //1、读取集合对象
        List<Student> studentList = get();
        for (int i = 0; i < studentList.size(); i++) {
            if(studentList.get(i).getId().equals(id)){
                return i;
            }
        }
        return -1;
    }
}
